// RealPlay Ltd  copyrights.
// This program is Open Source AsIs
// You may use it for all purposes, including commercial
// But!!!
// If you use any part of this code, you must state clearly
// both in your source file and in the Help|about of your program,
// that the origin program or code was built by RealPlay Ltd

// ***********************************************************************/
// *                                                                     */
// *     This unit is for basic file actionsfor  "Paint" Program         */
// *                                                                     */
// ***********************************************************************/
unit UPActions;

interface

uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Menus, shlobj, JPeg, gifImage;

procedure ActionFileNew;
procedure ActionFileOpen;
procedure ActionSave(b: Tbitmap; fn: string);
procedure ActionFileSave;
procedure ActionFileSaveAs;
procedure ActionEditCopy;
procedure ActionEditPaste;
procedure ActionViewZoomX1;
Procedure ActionRealAShow;
Procedure OpenFile(fname: string);

// **************************************************************
implementation

uses UPMain, UPMenu, UPUtil, UP_XML,
  Clipbrd;

Procedure OpenFile(fname: string);
var
  jpg: TJPEGImage;
  Gif: TGifImage;
  Aname: string;
begin
  with FPaint.FWA do
  begin
    Clear;
    ImSelS.ClearSel;
    with FPaint.FrTools1 do
      ToolSelect(sbNone);
    FMenu.opd1.FileName := fname;
    if (extFileext = '.JPG') or (extFileext = '.JPEG') then
    begin
      jpg := TJPEGImage.Create;
      try
        try
          jpg.CompressionQuality := 100; // Default Value
          jpg.LoadFromFile(fname);
          Im.Picture.bitmap.Assign(jpg);
        finally
          jpg.Free;
        end;
      except
        MessageDlg('JPEG file error!'#13'File not loaded', mtWarning, [mbOk],
          0);
      end;
    end
    else if (extFileext = '.GIF') then
    begin
      Gif := TGifImage.Create;
      try
        try
          Gif.LoadFromFile(fname);
          Im.Picture.bitmap.Assign(Gif);
        finally
          Gif.Free;
        end;
      except
        MessageDlg('GIF file error!'#13'File not loaded', mtWarning, [mbOk], 0);
      end;
    end
    else
      try
        Im.Picture.LoadFromFile(FMenu.opd1.FileName);
      except
        MessageDlg('File not loaded', mtWarning, [mbOk], 0);
      end;
    Im.Picture.bitmap.TransparentColor := clTrans;
    FMenu.AZoomx1Execute(nil);
    Mystat.Changed := false;
    Aname := ExtFileName + ' - Paint';
    if CheckIfAni then
      with FPaint do
      begin
        Aname := Aname + ' Animation';
        Fxml.OpenDialog1.FileName := changeFileExt(FMenu.opd1.FileName, '.xml');
        Fxml.OpenXMLfile;
        AniList.getlistfromXML(ExtractFilePath(FMenu.opd1.FileName));
        AniList.MakeMenu;
      end;
    Application.Title := Aname;
    FPaint.Caption := Aname;
  end;
end;

Procedure ActionRealAShow;
var
  StExe: AnsiString;
begin
  ActionFileSave;
  StExe := ExtractFilePath(Application.ExeName);
  with FPaint do
    with FWA do
      StExe := '"' + StExe + 'Animatiom.exe" "' + changeFileExt
        (FMenu.opd1.FileName, '.xml') + '" ' + inttostr(Im.Width)
        + ' ' + inttostr(Im.Height);
  WinExec(PAnsiChar(StExe), SW_SHOW);
end;

procedure ActionViewZoomX1;
begin
  ZoomXr := 1;
  ZoomYr := 1;
  FPaint.FWA.ImSelS.ClearSel;
  FPaint.FWA.Mystat.SetSize;
  {
    with FPaint.FWA do
    begin
    ImSelS.ClearSel;
    ImPanel.Height := Im.Picture.bitmap.Height + ImBottomPanel.Height;
    ImPanel.Width := Im.Picture.bitmap.Width + IMRightPanel.Width;
    ZoomXr := 1;
    Mystat.Cx := ZoomXr;
    ZoomYr := 1;
    Mystat.Cy := ZoomYr;
    end; }
end;

procedure ActionEditPaste;
begin
  FPaint.FWA.ImSelS.Paste;
end;

procedure ActionEditCopy;
begin
  with FPaint.FWA do
    if ImSelS.Visible then
      clipBoard.Assign(Im.Canvas.Brush.bitmap)
    else
      MessageDlg('You Must Select something', mtInformation, [mbOk], 0);
end;

procedure ActionFileSaveAs;
var
  fname: string;
begin
  with FPaint do
    with FMenu do
    begin
      FWA.ImSelS.ClearSel;
      SD.FileName := ExtFileName;
      SD.FilterIndex := 1;
      if (extFileext = '.JPG') or (extFileext = '.JPEG') then
        SD.FilterIndex := 3;
      if SD.Execute then
      begin
        if ExtractFileExt(SD.FileName) = '' then
          Case SD.FilterIndex of
            1, 2:
              fname := changeFileExt(SD.FileName, '') + '.bmp';
            3, 4:
              fname := changeFileExt(SD.FileName, '') + '.jpg';
            5:
              fname := changeFileExt(SD.FileName, '') + '.gif';
            6:
              fname := changeFileExt(SD.FileName, '') + '.tif';
            7:
              fname := changeFileExt(SD.FileName, '') + '.png';
          end;
        if fileexists(fname) and (MessageDlg('File ' + fname + #13 +
              'Exists!  Do you want to replace it?', mtWarning, [mbYes, mbNo],
            0) <> mrYes) then
          exit;
        writefilename(fname);
        opd1.FileName := fname;
        opd1.InitialDir := SD.InitialDir;
        ActionFileSave;
      end;
    end;
end;

// with FPaint
// ---------------------------------------------------------------
procedure ActionSave(b: Tbitmap; fn: string);
var
  Gif: TGifImage;
  jpg: TJPEGImage;
  ext: string;
begin
  ext := Uppercase(ExtractFileExt(fn));
  if (ext = '.JPG') or (ext = '.JPEG') then
  begin
    jpg := TJPEGImage.Create;
    try
      jpg.CompressionQuality := 100; // Default Value
      jpg.Assign(b);
      jpg.SaveToFile(fn);
    finally
      jpg.Free;
    end;
  end
  else if (ext = '.GIF') then
  begin
    Gif := TGifImage.Create;
    try
      Gif.Assign(b);
      Gif.SaveToFile(fn);
    finally
      Gif.Free;
    end;
  end
  else
    b.SaveToFile(fn);
  FPaint.FWA.ZF.GetN;
  FPaint.FWA.Mystat.Changed := false;
end;

// ---------------------------------------------------------------
procedure ActionFileSave;
var
  i: integer;
begin
  if FPaint.isAni then
  begin
    with FPaint do
      with AniList do
        for i := 0 to count - 1 do
          items[i].SaveToFile(ExtractFilePath(FMenu.opd1.FileName)
              + items[i].FileName);
    Fxml.XMLDoc.SaveToFile(Fxml.OpenDialog1.FileName);
  end
  else
    ActionSave(FPaint.FWA.Im.Picture.bitmap, FMenu.opd1.FileName);
  FPaint.FWA.ZF.GetN;
  FPaint.FWA.Mystat.Changed := false;
end;
// --------------------------------------------------------------------

procedure ActionFileOpen;
begin
  with FPaint do
  begin
    if SaveOrExit then
      exit;
    if FMenu.opd1.Execute then
    begin
      OpenFile(FMenu.opd1.FileName);
      writefilename(FMenu.opd1.FileName);
    end;
  end;
end;

// with FPaint
// ------------------------------ ---------------------------------------
procedure ActionFileNew;
var
  b: Tbitmap;
  i: integer;
  path: array [0 .. 80] of char;
const
  CSIDL_MYPICTURES = $0027;
begin
  with FPaint do
  begin
    FWA.ImSelS.ClearSel;
    FWA.Clear;
    if SaveOrExit then
      exit;
    ShGetSpecialFolderPath(0, path, CSIDL_MYPICTURES, false);
    FMenu.opd1.FileName := path + '\untiteled.bmp';
    i := 0;
    if fileexists(FMenu.opd1.FileName) then
    begin
      while fileexists(changeFileExt(FMenu.opd1.FileName,
          '') + inttostr(i) + '.bmp') do
        inc(i);
      FMenu.opd1.FileName := changeFileExt(FMenu.opd1.FileName, '') + inttostr
        (i) + '.bmp';
    end;
    Application.Title := ExtFileName + ' - Paint';
    FPaint.Caption := ExtFileName + ' - Paint';
    // the new
    b := Tbitmap.Create;
    b.Width := FWA.Im.Picture.Width;
    b.Height := FWA.Im.Picture.Height;
    FWA.Im.Picture.Assign(b);
    FWA.Im.Picture.bitmap.PixelFormat := pf24bit;
    FWA.Im.Picture.bitmap.TransparentColor := clTrans;
    b.Free;
    ZoomXr := 1;
    ZoomYr := 1;
    FWA.Mystat.SetSize;
    FWA.Mystat.Changed := false;
  end;
end; // with FPaint

end.
